package com.qf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.mapper.DtsAccountTraceMapper;
import com.qf.pojo.DtsAccountTrace;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

//佣金管理
@RequestMapping("/admin/brokerage")
@CrossOrigin
@RestController
public class AdminBrokerageController {

    @Autowired
    private DtsAccountTraceMapper dtsAccountTraceMapper;

    /**
     * 佣金列表
     * @param page
     * @param limit
     * @param username
     * @param mobile
     * @param statusArray
     * @param sort
     * @param order
     * @return
     *
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"total": 6,
     * 		"traceList": [
     * 			{
     * 				"id": 13,
     * 				"traceSn": "20191208972291",
     * 				"userId": 1,
     * 				"type": 0,
     * 				"amount": 175.28,
     * 				"totalAmount": 295.28,
     * 				"addTime": "2019-12-08 01:30:00",
     * 				"status": 0
     * 			},
     * 			{
     * 				"id": 14,
     * 				"traceSn": "20191208768873",
     * 				"userId": 2,
     * 				"type": 0,
     * 				"amount": 6.79,
     * 				"totalAmount": 6.79,
     * 				"addTime": "2019-12-08 01:30:00",
     * 				"status": 0
     * 			}
     * 		]
     * 	},
     * 	"errmsg": "成功"
     * }
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:brokerage:list")
    @RequiresPermissionsDesc(menu = "佣金管理",button = "佣金列表")
    public Object list(@RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       String username,String mobile,Integer [] statusArray,
                       String sort,String order){

        Page<DtsAccountTrace> pageHelp = new Page<>(page, limit);

        QueryWrapper<DtsAccountTrace> wrapper = new QueryWrapper<>();
        /*if (!StringUtils.isEmpty(username)){
            wrapper.eq("username", username);
        }*/
        if (!StringUtils.isEmpty(mobile)){
            wrapper.like("mobile", mobile);
        }
        /*if (statusArray.length != 0 && statusArray != null){
            wrapper.eq("status", statusArray);
        }*/
        wrapper.orderByAsc("asc".equalsIgnoreCase(order),sort);
        wrapper.orderByDesc("desc".equalsIgnoreCase(order),sort);

        Integer count = dtsAccountTraceMapper.selectCount(wrapper);

        IPage<DtsAccountTrace> iPage = dtsAccountTraceMapper.selectPage(pageHelp,wrapper);

        List<DtsAccountTrace> data = iPage.getRecords();

        DtsAccountTrace [] traceList = new DtsAccountTrace[data.size()];
        for (int i = 0; i < data.size(); i++) {
            traceList[i] = data.get(i);
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", count);
        hashMap.put("traceList", traceList);


        return ResponseUtil.ok(hashMap);
    }

    /**
     * 佣金审批
     * @return
     * {
     * 	"errno": 0,
     * 	"errmsg": "成功"
     * }
     */
    //佣金审批、	"errno":0、	"errmsg": "成功"
    @RequestMapping("/approve")
    @RequiresPermissions("admin:brokerage:approve")
    @RequiresPermissionsDesc(menu = "佣金管理",button = "佣金审批")
    public Object approve(){
        return ResponseUtil.ok();
    }
}
